/** 门店会员信息 */
import { useShopUserStoreWithOut } from '@/store/modules/shop/user'
import * as UserApi from '@/api/member/user'
import * as WalletApi from '@/api/pay/wallet/balance'

export function useMember() {
  const shopUserStore = useShopUserStoreWithOut()
  const shopUser = computed(() => {
    return shopUserStore.getUser
  })
  const isSetUser = computed(() => {
    return shopUserStore.getIsSetUser
  })
  const loading = ref(true) // 列表的加载中
  const total = ref(0) // 列表的总页数
  const list = ref([]) // 列表的数据
  const isShowList = ref(false)

  const queryParams = reactive({
    pageNo: 1,
    pageSize: 10,
    mobile: ''
  })
  /** 查询列表 */
  const getList = async () => {
    loading.value = true
    try {
      const data = await UserApi.getUserPage(queryParams)
      list.value = data.list
      total.value = data.total
      console.log(list.value)
    } finally {
      loading.value = false
    }
  }
  /* 用户钱包相关信息 */
  const WALLET_INIT_DATA = {
    balance: 0,
    totalExpense: 0,
    totalRecharge: 0
  } as WalletApi.WalletVO // 钱包初始化数据
  const wallet = ref<WalletApi.WalletVO>(WALLET_INIT_DATA) // 钱包信息

  /** 查询用户钱包信息 */
  const getUserWallet = async () => {
    if (!isSetUser.value) {
      wallet.value = WALLET_INIT_DATA
      return
    }
    const params = { userId: shopUser.value.id }
    wallet.value = (await WalletApi.getWallet(params)) || WALLET_INIT_DATA
  }
  watch(
    () => list.value,
    () => {
      console.log(list.value)
    }
  )

  return {
    shopUser,
    isSetUser,
    loading,
    total,
    list,
    isShowList,
    queryParams,
    wallet,
    getList,
    getUserWallet
  }
}
